<?php namespace system\model;
//	框架的核心模型类	
	use hdphp\model\Model;
	
//	模型类：商品表模型
	class Goods extends Model{
		protected $table='goods';
//		商品验证
		protected $validate=array(
			array('gname','required','商品名不能为空',3,3),
		);		
		
		public function store(){
			if(!$this->create()) return false;
//添加商品列表
//			图片转换
			$this->data['pic']=$this->data['thumb'];
//			当前时间
			$this->data['time']=time();
//			管理员
			$this->data['aid']=$_SESSION['id'];
//			写入数据库
			$gid=$this->add();
//添加商品属性表
//			添加商品属性数据
			$gattr=new \system\model\goods_attr;
			$attr=$this->data['attr'];
			if($attr){
				foreach ($attr as $k => $v) {
					if(is_null($v)){
						$gattr->add(array('gtvalue'=>$v,'taid'=>$k,'gid'=>$gid));
					}
				}
			}
			
//			添加商品规格数据
			$spec=$this->data['spec'];
			foreach($spec as $k => $v){
				foreach($v['gtvalue'] as $kk=>$vv){
//					$this->add(array('gtvalue'=>$k[$vv]));
//					$arr[]=array(
//					'gtvalue'=>$vv,
//					'gid'=>$gid,
//					'taid'=>$k,
//					'added'=>$v['added'][$kk]
//					);
					$gattr->add(array('gtvalue'=>$vv,'taid'=>$k,'gid'=>$gid,'added'=>$v['added'][$kk]));
				}
			}
			

//添加详情页
// [photo] => Array
//      (
//          [0] => attachment/2016/09/28/79191475061563.jpg
//          [1] => attachment/2016/09/28/97481475055764.png
//          [2] => attachment/2016/09/28/79191475061563.jpg
//          [3] => attachment/2016/09/28/97481475055764.png
//      )

//			小图
			foreach($this->data['photo'] as $k=>$v){
				$xv=str_replace('.', '_58X58.', $v);
//				p($v);
//				p($xv);die;
				$re=Image::thumb($v,$xv,58,58,6);
				
				$small[]=$re;
				
			}
			$small=implode('|', $small);
//			中图
			foreach($this->data['photo'] as $k=>$v){
				$xv=str_replace('.', '_480X480.', $v);
//				p($xv);
				$re=Image::thumb($v,$xv,480,480,6);
				$medium[]=$re;
				
			}
			$medium=implode('|', $medium);			
//			大图
			foreach($this->data['photo'] as $k=>$v){
				$xv=str_replace('.', '_900X900.', $v);
//				p($xv);
				$re=Image::thumb($v,$xv,900,900,6);
				$big[]=$re;
				
			}
			$big=implode('|', $big);			
//			p($small);
//			p($medium);
//			p($big);
//			写入表
			$detail=new \system\model\detail;
			$detail->add(array('small'=>$small,'medium'=>$medium,'big'=>$big,'intro'=>$this->data['intro'],'service'=>$this->data['service'],'gid'=>$gid));
			
			
			return ture;
//			die;
		}
		
//		public function edit(){
//			if(!$this->create()) return false;
//			$this->data['logo']=$this->data['thumb'];
//			$re=$this->save();
//			if(!$re){
//				$this->error='编辑失败，请重新保存';
//				return false;
//			}		
//			return true;
//		}
	}